package com.hsd.whb.service.impl;


import com.hsd.whb.properties.PersonProperties;
import com.hsd.whb.service.MailSenderService;
import com.hsd.whb.utils.FileUtil;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Service
public class MailSenderServiceImpl implements MailSenderService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private PersonProperties personProperties;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public void sendMail(String to, String subject, String msg) {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom(personProperties.getMail());
        mail.setTo(to);
        mail.setSubject(subject);
        mail.setText(msg);
        mailSender.send(mail);
    }


    @Override
    public void sendMail(String from, String to, String subject, String msg) {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom(from);
        mail.setTo(to);
        mail.setSubject(subject);
        mail.setText(msg);
        mailSender.send(mail);
    }

    @Override
    public void sendMail(String to, String subject, String msg, File[] files) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(personProperties.getMail());
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(msg);
        for (File file : files) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
            }
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, File[] files) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(msg);
        for (File file : files) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
            }
        }
        mailSender.send(mimeMessage);
    }


    @Override
    public void sendMail(String to, String subject, String msg, Object model) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(personProperties.getMail());
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(getTemplateText(msg, model), true);
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, Object model) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(getTemplateText(msg, model), true);
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String to, String subject, String msg, File[] files, Object model) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(personProperties.getMail());
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(getTemplateText(msg, model), true);
        System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
        for (File file : files) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
            }
        }
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String from, String to, String subject, String msg, File[] files, Object model) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(getTemplateText(msg, model), true);
        System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
        for (File file : files) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                mimeMessageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), new FileSystemResource(file));
            }
        }
        mailSender.send(mimeMessage);
    }

    private String getTemplateText(String path, Object model) throws Exception {
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(path);
        String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        return htmlText;
    }
}
